<template>
  <div>
    <!-- 整个页面需要显示内容的区域 -->
    <div class="container">
      <!-- 顶部区域 -->
      <div class="header">
        <h1>中医养生馆管理平台</h1>
      </div>
      <!-- 登录表单 -->
      <div class="login-form">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
          <el-form-item label="" prop="username">
            <el-input v-model="ruleForm.username">
              <template slot="prepend"><i class="el-icon-user-solid"/></template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="ruleForm.password">
              <template slot="prepend"><i class="el-icon-s-ticket"/></template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;"
                       @click="submitForm('ruleForm')">登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 版权信息 -->
      <div class="copyright">
        <p>Copyright &copy; 中医养生馆 | 研发部</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'root',
        password: '123456'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '用户名的长度必须是 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '密码的长度必须是 4 到 15 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = BaseUrl.PASSPORT + '/passport/login';
          console.log('url = ' + url);

          SimpleAxios.post(url, this.ruleForm).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              let loginInfo = jsonResult.data;
              localStorage.setItem('loginInfo', JSON.stringify(loginInfo));
              this.$router.push('/');
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          }).catch(error => {
            let title = '错误';
            let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
            this.$alert(message, title, {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
            console.log(error);
          });
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style>
body {
  height: 100vh;
  background-image: linear-gradient(to bottom left, #5abae8, #3c4bd2);
  background-repeat: no-repeat;
}

a {
  color: #3c4bd2;
  text-decoration: none;
}

a:hover {
  color: #212a79;
  text-decoration: underline;
}

.container {
  width: 460px;
  margin: 0 auto;
}

.header {
  margin-top: 50px;
}

.header h1 {
  text-align: center;
  color: #fff;
}

.login-form {
  background: #fff;
  border-radius: 10px;
  padding: 50px 50px 20px 50px;
}

.copyright {
  margin-top: 20px;
}

.copyright p {
  text-align: center;
  font-size: 11px;
  color: #fff;
}
</style>